﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace TinyEngine.Editor.Extensibility.Controls
{
    public class BlendStyleTextbox : TextBox
    {
        public partial class NativeMethods
        {
            /// Return Type: BOOL->int  
            ///X: int  
            ///Y: int  
            [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetCursorPos")]
            [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
            public static extern bool SetCursorPos(int X, int Y);
        } 
        public BlendStyleTextbox()
        {
        }
        Point lastPoint;
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                isChangingValue = true;
                lastPoint = this.PointToScreen(e.GetPosition(this));

            }
            base.OnMouseDown(e);
        }
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            if (isChangingValue)
            {
                var pnt = this.PointToScreen(e.GetPosition(this));
                double dbl = 0.0;
                double.TryParse(this.Text, out dbl);

                if (pnt.X < lastPoint.X)
                {
                    dbl--;
                }
                else if (pnt.X > lastPoint.X)
                {
                    dbl++;
                }
                else if (pnt.Y > lastPoint.Y)
                {
                    dbl++;
                }
                else if (pnt.Y < lastPoint.Y)
                {
                    dbl--;
                }
                this.Text = dbl.ToString();
                e.Handled = true;
                NativeMethods.SetCursorPos((int)lastPoint.X, (int)lastPoint.Y);

            }
            base.OnMouseMove(e);
        }
        protected override void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            isChangingValue = false;
            base.OnMouseUp(e);
        }
        bool isChangingValue;
    }
}
